package Work0608;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SocketServer extends Thread {
	private Socket s;

	public SocketServer(Socket s) {
		this.s = s;
		super.setName(String.valueOf(s.getInetAddress()));
	}

	public void run() {
		Date date;
		System.out.println(s.getInetAddress() + ":" + s.getPort() + "连接上了...");
		String str = null;
		try {
			InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			BufferedReader i = new BufferedReader(new InputStreamReader(in));
			PrintWriter o = new PrintWriter(out);
			String st = "yyyy-MM-dd hh:mm:ss";
			SimpleDateFormat df;
			String day;
			while (true) {
				str = i.readLine();
				date = new Date();
				df = new SimpleDateFormat(st);
				day = df.format(date);
				if (str.equals("1")) {
					o.println("气温36°c\t" + day);
				} else if (str.equals("2")) {
					o.println("风力3级\t" + day);
				} else if (str.equals("3")) {
					o.println("pm2.5为35μg/m³\t" + day);
				} else if (str.equals("4")) {
					o.println("湿度为40%\t" + day);
				} else if (str.equals("5")) {
					o.println("结束");

				}
				o.flush();
			}

		} catch (Exception e) {
			System.out.println("出错了");
			e.printStackTrace();
		} finally {
			if (str.equals("5")) {
				try {
					s.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		}

	}

}
